package com.hongkuncheng.cms.controller.admin;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.model.Form;
import com.hongkuncheng.cms.model.FormField;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 表单管理
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/form")
public class AformController extends BaseController {

    // 表单列表
    @RequestMapping
    public String index() {
        List<Form> forms = factorySession.createCriteria(Form.class).addOrder(Order.asc("sort")).list();
        request.setAttribute("forms", forms);
        return view();
    }

    // 表单编辑
    @RequestMapping("edit")
    public String edit() {
        int formId = request.getParameter("formid") == null ? 0 : Integer.parseInt(request.getParameter("formid"));
        Form form = formId == 0 ? new Form() : factorySession.get(Form.class, formId);
        if (formId > 0 && form.getNotice() != null) form.setNotice(form.getNotice().replace("<br/>", "\r\n"));
        request.setAttribute("form", form);
        return view(false);
    }

    // 表单编辑处理
    @RequestMapping("editdo")
    @ResponseBody
    public void editdo() {
        checkAdminPermission("yunwei_webmaintain_form");
        int formId = Integer.parseInt(request.getParameter("formid"));
        Form form = formId == 0 ? new Form() : factorySession.load(Form.class, formId, LockMode.PESSIMISTIC_WRITE);
        form.setName(request.getParameter("name"));
        form.setAction(request.getParameter("action"));
        form.setNotice(request.getParameter("notice").isEmpty() ? null :
                request.getParameter("notice").replace("\r\n", "<br/>"));
        form.setSort(Integer.parseInt(request.getParameter("sort")));
        if (formId == 0) factorySession.save(form);
        recordLog((formId == 0 ? "新建" : "更新") + "表单《" + form.getName() + "》");
        PageHelper.alertAndRedirect("保存成功", "./");
    }

    // 表单设计
    @RequestMapping("design")
    public String design() {
        int formId = Integer.parseInt(request.getParameter("formid"));
        Form form = factorySession.get(Form.class, formId);
        request.setAttribute("form", form);
        List<FormField> formfields = factorySession.createCriteria(FormField.class)
                .add(Restrictions.eq("form.id", formId))
                .addOrder(Order.asc("indexed")).list();
        request.setAttribute("formfields", formfields);
        return view();
    }

    // 表单设计处理
    @RequestMapping("designdo")
    @ResponseBody
    public void designdo() {
        checkAdminPermission("yunwei_webmaintain_form");
        int formId = Integer.parseInt(request.getParameter("formid"));
        Form form = factorySession.load(Form.class, formId, LockMode.PESSIMISTIC_WRITE);
        form.setDesign(request.getParameter("design"));
        recordLog("设计表单《" + form.getName() + "》");
        PageHelper.alertAndRedirect("保存成功", "./");
    }

    // 表单删除
    @RequestMapping("delete")
    @ResponseBody
    public void delete() {
        checkAdminPermission("yunwei_webmaintain_form");
        int formId = Integer.parseInt(request.getParameter("formid"));
        Form form = factorySession.load(Form.class, formId, LockMode.PESSIMISTIC_WRITE);
        factorySession.delete(form);
        recordLog("删除表单《" + form.getName() + "》");
        PageHelper.alertAndRedirect("删除成功", "./");
    }

}
